from Group import GroupBase
from AttributeGroup import groupClasses

class DeviceGroup(GroupBase):
    _saved_class_name = 'DeviceGroup'
    _saved_attributes = ['group_keys']
    _saved_child_objects = ['AttributeGroups']
    def __init__(self, **kwargs):
        if 'deserialize' not in kwargs:
            self.AttributeGroups = {}
            #self.member_groups = {}
            self.group_keys = {}
            for key, val in groupClasses.iteritems():
                self.AttributeGroups.update({key:val(parent_group=self)})
                #self.member_groups.update({key:{}})       
        
        super(DeviceGroup, self).__init__(**kwargs)
        devices = kwargs.get('devices', [])
        self.add_devices(devices)
        
    def add_devices(self, devices):
        if type(devices) == list or type(devices) == tuple:
            for device in devices:
                self.add_member(**{device.id:device})
        else:
            self.add_member(**{devices.id:devices})
            
    def add_member(self, **kwargs):
        super(DeviceGroup, self).add_member(**kwargs)
        
        for key, val in kwargs.iteritems():
            self.group_keys.update({key:{}})
            #self.update_member_groups(key)
            for grkey, grval in self.members[key].Groups.iteritems():
                self.group_keys[key].update({grkey:grval.id})
                self.AttributeGroups[grkey].add_member(**{grval.id:grval})
                print self.AttributeGroups[grkey]
            
    def del_member(self, key):
        if key in self.members:
            for grkey, grval in self.AttributeGroups.iteritems():
                group_name = self.group_keys[key].get(grkey)
                if group_name:
                    grval.del_member(group_name)
        super(DeviceGroup, self).del_member(key)
        
    def get_values(self):
        d = {}
        for key, val in self.AttributeGroups.iteritems():
            d.update({key:val.get_values()})
        return d
        
    def get_devices(self):
        d = {}
        for key, val in self.members.iteritems():
            if getattr(val, 'base_class') == 'PatchedDevice':
                d.update({key:val})
            elif getattr(val, 'base_class') == 'Group':
                d.update(val.get_devices())
        return d
        
    def get_objects(self, d={}):
        for key in ['DeviceGroup', 'AttributeGroup', 'Device', 'Attribute']:
            if key not in d:
                d.update({key:{}})
        d['DeviceGroup'].update({self.id:self})
        for group in self.AttributeGroups.itervalues():
            group.get_objects(d)
        for key, val in self.members.iteritems():
            if val.saved_class_name == 'PatchedDevice':
                d['Device'].update({key:val})
        return d
        
    def search_for_obj(self, objkey):
        if objkey in self.AttributeGroups:
                return self.AttributeGroups[objkey]
        for group in self.AttributeGroups.itervalues():
            result = group.search_for_obj(objkey)
            if result:
                return result
        return super(DeviceGroup, self).search_for_obj(objkey)
            
    def _deserialize_child(self, d):
        for cls in groupClasses.itervalues():
            if cls._saved_class_name == d['saved_class_name']:
                return cls(parent_group=self, deserialize=d)
